package com.yanqu.road.web.controller.chargecenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.MallRechargeConfigMgr;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.web.mgr.MallMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/rechargeConditionList")
public class RechargeConditionList extends BaseJsonHttpServlet {

    private static Logger logger = LogManager.getLogger(RechargeConditionList.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request rechargeConditionList");
        HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
        JSONObject paramObj = httpJsonWebParam.getParamObj();
        Map<String, MallInfo> infoMap = MallMgr.getRechargeCondition();
        RechargeConditionResult conditionResult = new RechargeConditionResult();
        Map<String, RechargeCondition> data = new HashMap<>();

        for (MallInfo info : infoMap.values()) {
            RechargeCondition condition = new RechargeCondition();
            condition.setId(String.valueOf(info.getRechargeId()));
            condition.setName(MultipleLanguageMgr.getContent(info.getMallName(),"zh_cn"));
            condition.setDesc(MultipleLanguageMgr.getContent(info.getMallName(),"zh_cn"));
            int price = (int) MallRechargeConfigMgr.getPrice(info);
            condition.setMoney(price / 100);
            condition.setStatu(1);
            data.put(info.getRechargeId()+"", condition);
        }
        conditionResult.setState(1);
        conditionResult.setData(data);
        String result = JSON.toJSONString(conditionResult);
        sendMessage(response, result);
    }
}
